﻿/**********************************************
系统:Ebada物流企业ERP
模块:示例
作者:Rabbit
创建时间:2011-2-28
最后一次修改:2011-2-28
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;

namespace Ebada.Modules.Document
{

    public partial class UCDocumentsReturn : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<Documents> gridViewOperation;

        public event SendDataEventHandler<Documents> FocusedRowChanged;
        public string cztype = "";
        public UCDocumentsReturn()
        {
            InitializeComponent();
           
           // initColumns();
            gridViewOperation = new GridViewOperation<Documents>(gridControl1, gridView1, barManager1);
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewOperation_FocusedRowChanged);
            Add_columnName();
            Init_Org();
            initColumns();
        
        }

        void gridViewOperation_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) 
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as Documents);
        }
        public void Show_Style(string Style)
        {
            if (Style == "归还")
            {
                barBtnOut.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                cztype = "领取";
                //this.barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                //this.barButtonItem3.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
            else if (Style == "移入")
            {
                barBtnLq.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                cztype = "移出";
            }
        }
        /// <summary>
        /// 设置隐藏列
        /// </summary>
       public  void initColumns() 
        {
            //隐藏单证前3列
            for (int i = 0; i < 4; i++)
            {
                gridView1.Columns[i].Visible = false;
            }
            gridView1.Columns["OrgName"].Caption = "责任单位名称";
            gridView1.Columns["DocState"].Caption = "单证状态";
            gridView1.Columns["DocState"].Visible = false;
            gridView1.Columns["DocStateName"].Caption = "单证状态";
            gridView1.Columns["DocCode"].Caption = "单证编号";
            gridView1.Columns["Act_Date"].Caption = "作业时间";
            gridView1.Columns["SubmitDateLimit"].Caption = "单证提交时限";
            gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
            gridView1.Columns["ValidityDate"].Caption = "有效截至期";
            gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
            gridView1.Columns["CheckResult"].Caption = "审查结果";
            gridView1.Columns["CheckRemark"].Caption = "审查说明";
            gridView1.Columns["CheckUserID"].Caption = "审查人ID";
            gridView1.Columns["CheckUserName"].Caption = "审查人";
            gridView1.Columns["CheckedDate"].Caption = "审查时间";
            gridView1.Columns["Org_ID"].Caption = "承运商";
            gridView1.Columns["Org_ID"].Visible = false;
            gridView1.Columns["SpliteFlag"].Visible = false;
            gridView1.Columns["ScrapDate"].Caption = "报废时间";
            gridView1.Columns["DocBookID"].Caption = "册子编号";
            gridView1.Columns["DocBookName"].Caption = "册子名称";
            gridView1.Columns["DocBookSeq"].Caption = "册子里序列号";
            gridView1.Columns["CreateUserID"].Caption = "创建人ID";
            gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
           
        }
        //隐藏菜单
       public void Hide_Find()
       {
           this.btFind.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barBtnLq.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           //this.barBtnMove.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barBtnOut.Visibility = DevExpress.XtraBars.BarItemVisibility.Never; 
       }
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
        public GridViewOperation<Documents> GridViewOperation {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(Documents newobj) {
            newobj.DocumentID = Guid.NewGuid().ToString();
           
        }
        /// <summary>
        /// 父表ID
        /// </summary>
        [Browsable(false)]
       //根据库位信息刷新单证
        public void RefreshData(List<string> str_list)
        {
            string str = "where 2>1";
            if (str_list.Count>0)
            {
                str = "where DocumentPositionID=";
                for (int i = 0; i < str_list.Count; i++)
                {
                    str += "'" + str_list[i] + "'";
                    str += " or DocumentPositionID=";
                }
                string str2 = " or DocumentPositionID=";
                str = str.Substring(0, str.Length - str2.Length);
            }
            gridViewOperation.RefreshData(str);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.Site == null)
                InitData();
                //initdatatype(cztype);
           
        }
        public void HideColums()
        {
           // gridView1.Columns["DocState"].Visible = false;
            gridView1.Columns["DocCode"].Visible = false;
            gridView1.Columns["DocumentTypeID"].Visible = false;
            gridView1.Columns["DocumentPositionID"].Visible = false;
            gridView1.Columns["DocState"].Visible = false;
        }
        public void InitData()
        {
            gridViewOperation.RefreshData("Where DocumentID!=''and DocState ='2'");
            HideColums();
        }
        public void initdatatype(string type)
        {
             switch (type)
             {
                 case "领取":
                   string con = ",DocumentMoveInfo where Document.DocumentID!=''and Document.DocState ='2' and DocumentMoveInfo.DocumentID=Document.DocumentID and DocumentMoveInfo.OperateType='领取'";
                   gridViewOperation.RefreshData(con);
             	break;
                 case"移出":
                     con = ",DocumentMoveInfo where Document.DocumentID!=''and Document.DocState ='2' and DocumentMoveInfo.DocumentID=Document.DocumentID and DocumentMoveInfo.OperateType='移出'";
                    gridViewOperation.RefreshData(con);
                     break;
                 default:
                  gridViewOperation.RefreshData("Where DocumentID!=''and DocState ='2'");
                     break;

             }
             HideColums();

        }
        //单证ID
        private string documentID=string.Empty;
        private string DocumentID
        {
            set { documentID = value; }
            get { return documentID; }
        }
        //单证名称
        private string documentName = string.Empty;
        private string DocumentName
        {
            set { documentName = value; }
            get { return documentName; }
        }
        //单证位置
        private string documentPositionID = string.Empty;
        private string DocumentPositionID
        {
            set { documentPositionID = value; }
            get { return documentPositionID; }
        }
        //归还操作
        private void barBtnLq_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add_DocumentMoveInf("返还");
        }
        //移入操作
        private void barBtnOut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add_DocumentMoveInf("入库");
        }
        //移出操作
        //private void barBtnMove_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        //{
        //    Add_DocumentMoveInf("3");
        //}
        //将关联单位标识改为关联单位
        //并以中文显示
        public void Init_Org()
        {
            gridView1.Columns["Org_ID"].Caption = "关联单位";
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["Org_ID"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            comboBox.DataSource = org_list;
            comboBox.DisplayMember = "cnName";
            comboBox.ValueMember = "Org_ID";

        }
        public void Add_columnName()
        {
            gridView1.Columns["DocumentTypeName"].Caption = "单证种类";
            gridView1.Columns["DocState"].Caption = "单证状态";

            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["DocState"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            DataTable temptable = new DataTable();
            temptable.Columns.Add("code", typeof(int));
            temptable.Columns.Add("Name", typeof(string));
            DataRow row1 = temptable.NewRow();
            DataRow row2 = temptable.NewRow();
            DataRow row3 = temptable.NewRow();
            DataRow row4 = temptable.NewRow();
            DataRow row5 = temptable.NewRow();
            row1["code"] = 0;
            row1["Name"] = "未提交";

            row2["code"] = 1;
            row2["Name"] = "提交";

            row3["code"] = 2;
            row3["Name"] = "借出";

            row4["code"] = 3;
            row4["Name"] = "报废";

            row5["code"] = 4;
            row5["Name"] = "移出";
            temptable.Rows.Add(row1);
            temptable.Rows.Add(row2);
            temptable.Rows.Add(row3);
            temptable.Rows.Add(row4);
            temptable.Rows.Add(row5);

            comboBox.DataSource = temptable;
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "code";
        }
        
        private void Add_DocumentMoveInf(string operatortype)
        {
            if (DocumentID == string.Empty)
            {
                MessageBox.Show("请选择单证进行操作");
                return;
            }
            DocumentMoveInfo obj = new DocumentMoveInfo();
            string con = "where DocumentID='" + DocumentID + "'and OperateType in('领取','移出')";
            IList<DocumentMoveInfo> list = MainHelper.PlatformSqlMap.GetListByWhere<DocumentMoveInfo>(con);
            if (list.Count>0)
            {
                obj = list[0];
                obj.InUserDate = DateTime.Now;
                obj.OperateType = operatortype;
            }
            else
            {
                obj.DocumentMoveInfoID = obj.CreateID();
                obj.DocumentID = DocumentID;
                obj.OperateType = operatortype;
                obj.InUserDate = DateTime.Now;
                obj.OutPosition = DocumentPositionID;
            }
            
            frmDocumentMoveInfoEdit frm = new frmDocumentMoveInfoEdit();
            
           
            switch (operatortype)
            {
                //根据操作在编辑窗口显示相应信息
                case "返还": 
                    frm.Operator_Text = "归还单证  " + DocumentName + "  登记信息 ";
                    obj.InPosition = obj.OutPosition;
                    //领取位置即为单证存放位置
                    //obj.OutPosition = DocumentPositionID;
                    break;
                case "入库": 
                    frm.Operator_Text = "移入单证  " + DocumentName + "  登记信息 ";
                    frm.returnormoveflag = true;
                    //借出位置即为单证存放位置
                    //obj.OutPosition = DocumentPositionID;
                    break;
                //case "3": 
                //    frm.Operator_Text = "移出单证  " + DocumentName + "  登记信息 ";
                //    //移出位置即为单证存放位置
                //    obj.OutPosition = DocumentPositionID;
                //    break;
                default:
                    break;
            }
            frm.RowData = obj;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if (list.Count>0)
                    {
                        MainHelper.PlatformSqlMap.Update<DocumentMoveInfo>(obj);

                    }
                    else
                        MainHelper.PlatformSqlMap.Create<DocumentMoveInfo>(obj);
                    Documents obj_Doc= MainHelper.PlatformSqlMap.GetOneByKey<Documents>(DocumentID);
                    //根据操作修改单证状态 2为借出状态
                    switch (operatortype)
                    {
                        //领取
                        case "1":
                        //归还
                        case "返还":
                            obj_Doc.DocState = 1;
                            obj_Doc.DocStateName = "提交";
                            MainHelper.PlatformSqlMap.Update<Documents>(obj_Doc);
                            InitData();
                            break;
                        //移入
                        case "入库":
                            //更新单证新的位置，即为移入位置
                            obj_Doc.DocState = 1;
                            obj_Doc.DocStateName = "提交";
                            obj_Doc.DocumentPositionID = obj.InPosition;
                            MainHelper.PlatformSqlMap.Update<Documents>(obj_Doc);
                            InitData();
                            break;
                           
                        default:
                            break;
                    }
                    if (obj.CheckResult == "返回异常—丢失")
                    {
                        //重新创建单证 和修改返还单证的关联
                        frmDocumentsEdit newdoc = new frmDocumentsEdit();
                        newdoc.bdflag = true;
                        Documents dc = obj_Doc;
                        dc.DocumentID = dc.CreateID();
                        newdoc.RowData = dc;
                        newdoc.Text = "补单";
                        if (newdoc.ShowDialog() == DialogResult.OK)
                      {
                            //创建更新的历史
                          DocumentHistory dh = new DocumentHistory();
                          //dh.DocumentHisID = System.Guid.NewGuid().ToString();
                          //FuncLib.documentWritedocumenthistory(ref dh, obj_Doc);
                          //MainHelper.PlatformSqlMap.Create<DocumentHistory>(dh);
                            //将其丢失的单证设为报废
                          obj_Doc.ScrapDate = DateTime.Now;
                          obj_Doc.DocState = 3;
                          MainHelper.PlatformSqlMap.Update<Documents>(obj_Doc);
                            //重新补单
                          dc.DocState = 1;
                          MainHelper.PlatformSqlMap.Create<Documents>(dc);
                            //补上新单的历史
                         // dh.DocumentHisID = System.Guid.NewGuid().ToString();
                          FuncLib.documentWritedocumenthistory(ref dh, dc);
                          MainHelper.PlatformSqlMap.Create<DocumentHistory>(dh);
                            //修改单证移动信息
                          obj.DocumentID = dc.DocumentID;
                          obj.OperateType = operatortype;
                          obj.OutPosition = dc.DocumentPositionID;
                          MainHelper.PlatformSqlMap.Update<DocumentMoveInfo>(obj);
                          InitData();
                      }
                        
                    }
                    MessageBox.Show("操作已完成！");
                }
                catch (Exception e)
                {
                    MainHelper.ShowException(e);
                }
            }            
        }
        
        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
           
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                 
                Documents dc = view.GetRow(e.RowHandle) as Documents;
               
               // string DocumentID = view.GetRow(e.RowHandle) .GetRowCellValue(e.RowHandle, view.Columns[" DocumentID "]);
                //string DocumentID = view.GetFocusedRow().GetRowCellDisplayText(e.RowHandle, view.Columns[" DocumentID "]);
                string con = "where DocumentID='" + dc.DocumentID + "'and OperateType in('领取','移出')";
                IList<DocumentMoveInfo> list = MainHelper.PlatformSqlMap.GetListByWhere<DocumentMoveInfo>(con);
                if (list.Count>0)
                {
                    DocumentMoveInfo dm = list[0];
                    if (dm.OperateType == "领取")
                    {
                        //view.SetRowCellValue(e.RowHandle, view.Columns["DocState"], "5");
                        e.Appearance.BackColor = Color.LightSkyBlue;
                    }
                    if (dm.OperateType == "移出")
                    {
                       // view.SetRowCellValue(e.RowHandle, view.Columns["DocState"], "4");
                        e.Appearance.BackColor = Color.Yellow; 
                    }
                  
                }
              
            }

        }
        private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (gridView1.GetFocusedRowCellValue("DocumentID") != null)
            {
                DocumentID = gridView1.GetFocusedRowCellValue("DocumentID").ToString();
                DocumentName = gridView1.GetFocusedRowCellValue("DocumentTypeName").ToString();
                DocumentPositionID = gridView1.GetFocusedRowCellValue("DocumentPositionID").ToString();
                string con = "where DocumentID='" + DocumentID + "'and OperateType in('领取','移出')";
                IList<DocumentMoveInfo> list = MainHelper.PlatformSqlMap.GetListByWhere<DocumentMoveInfo>(con);
                if (list.Count > 0)
                {
                   DocumentMoveInfo dm = list[0];
                   if (dm.OperateType == "领取")
                   {
                       this.barBtnOut.Enabled = false;
                       this.barBtnLq.Enabled = true;
                   }
                   if (dm.OperateType == "移出")
                   {
                       this.barBtnLq.Enabled = false;
                       this.barBtnOut.Enabled = true;
                   }
                }
            }

           
        }
    }
}
